package cn.inst.common.security;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

import cn.inst.common.SecurityUntils;

public class CustomCredentialsMatcher extends SimpleCredentialsMatcher{
    
	/**
	 * 方法重写
	 * 定义登录方法
	 * token:来自页面的数据
	 * info:来自数据库的数据
	 */
	public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
		try {
			UsernamePasswordToken usernamePasswordToken=(UsernamePasswordToken) token;
			String password=String.valueOf(usernamePasswordToken.getPassword());
			
			Object tokenCredentials = SecurityUntils.MD5Password(password);
			Object accountCredentials = getCredentials(info);
			return equals(tokenCredentials, accountCredentials);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return false;
        
        
    }
}
